package com.atguigu.gmall.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import springfox.documentation.spring.web.json.Json;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ManageServiceImpl implements ManageService {
    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;

    public List<BaseCategory1> getCategory1() {
        return (baseCategory1Mapper.selectList(null));
    }

    public List<BaseCategory2> getCategory2(Long category1_id) {
        return baseCategory2Mapper.selectList(new QueryWrapper<BaseCategory2>().eq("category1_id", category1_id));
    }

    public List<BaseCategory3> getCategory3(Long category2_id) {
        return baseCategory3Mapper.selectList(new QueryWrapper<BaseCategory3>().eq("category2_id", category2_id));
    }

    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        return baseAttrInfoMapper.selectBaseAttrInfoList(category1Id, category2Id, category3Id);
    }

    @Override
    public List<BaseAttrValue> getBaseAttrValue(Long baseAttrInfoId) {
        BaseAttrInfo baseAttrInfo = baseAttrInfoMapper.selectById(baseAttrInfoId);
        if (baseAttrInfo != null) {
            List<BaseAttrValue> attr_id = baseAttrValueMapper.selectList(new QueryWrapper<BaseAttrValue>().eq("attr_id", baseAttrInfoId));
            baseAttrInfo.setAttrValueList(attr_id);
            return baseAttrInfo.getAttrValueList();
        }
        return null;
    }

    ////不写的话,默认是运行时异常回滚,受检异常不会滚,
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateBaseAttrInfo(BaseAttrInfo baseAttrInfo) {
        if (baseAttrInfo.getId() != null) {
            ////记住这个方法,别用update(T,QueryWrapper)
            baseAttrInfoMapper.updateById(baseAttrInfo);
        } else {
            baseAttrInfoMapper.insert(baseAttrInfo);
        }


        ////注意,遍历之前要验证是否为空,否则空指针
        if (!CollectionUtils.isEmpty(baseAttrInfo.getAttrValueList())) {
            baseAttrValueMapper.delete(new QueryWrapper<BaseAttrValue>().eq("attr_id", baseAttrInfo.getId()));
            for (BaseAttrValue item : baseAttrInfo.getAttrValueList()) {
                item.setAttrId(baseAttrInfo.getId());
                baseAttrValueMapper.insert(item);
            }
        }


    }

    @Autowired
    BaseCategoryViewMapper baseCategoryViewMapper;

    /*
    *
    CREATE VIEW base_category_view AS
        select
        c3.id as id,
        c1.id as category1_id, c1.name as category1_name,
        c2.id as category2_id, c2.name as category2_name,
        c3.id as category3_id, c3.name as category3_name
         FROM base_category1 c1 INNER JOIN base_category2 c2 on c2.category1_id = c1.id INNER JOIN base_category3 c3 on c3.category2_id = c2.id
    * */

    @Override
    public BaseCategoryView getCategoryViewByCategory3Id(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);
    }

    @Autowired
    SpuSaleAttrMapper spuSaleAttrMapper;
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(Long skuId, Long spuId) {
        return spuSaleAttrMapper.getSpuSaleAttrListCheckBySku(skuId,spuId);
    }

    //首页数据
    @Override
    public List<JSONObject> getBaseCategoryList() {
        List<JSONObject> result = new ArrayList<>();

        List<BaseCategoryView> all = baseCategoryViewMapper.selectList(null);
        Map<Long, List<BaseCategoryView>> collect1 = all.stream()
                                                       .collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        int index=0;
        for (Map.Entry<Long, List<BaseCategoryView>> entry : collect1.entrySet()) {
            JSONObject jsonItem = new JSONObject();
            Long c1Id = entry.getKey();
            List<BaseCategoryView> c1List = entry.getValue();
            jsonItem.put("index",++index);
            jsonItem.put("categoryName",c1List.get(0).getCategory1Name());
            jsonItem.put("categoryId",c1Id);


            ArrayList<JSONObject> part1 = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> collect2 = c1List.stream()
                                                            .collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

            for (Map.Entry<Long, List<BaseCategoryView>> entry2 : collect2.entrySet()) {
                JSONObject jsonItem2 = new JSONObject();
                Long c2Id = entry2.getKey();
                List<BaseCategoryView> c2List = entry2.getValue();
                jsonItem2.put("categoryName",c2List.get(0).getCategory2Name());
                jsonItem2.put("categoryId",c2Id);
                ArrayList<JSONObject> part2 = new ArrayList<>();
                for (BaseCategoryView baseCategoryView : c2List) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("categoryName",baseCategoryView.getCategory3Name());
                    jsonObject.put("categoryId",baseCategoryView.getCategory3Id());
                    part2.add(jsonObject);
                }
                jsonItem2.put("categoryChild",part2);
                part1.add(jsonItem2);
            }

            jsonItem.put("categoryChild",part1);

            result.add(jsonItem);
        }

        return result;
    }

    /**
     * 根据skuId获取平台属性键值
     * @param skuId
     * @return
     */
    @Override
    public List<BaseAttrInfo> getAttrList(Long skuId) {

        return baseAttrInfoMapper.selectBaseAttrInfoListBySkuId(skuId);
    }


}
